$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        titleRow:null,
        tabRow:null,
        init:function () {
            this.titleRow = $('#titleRow');
            this.tabRow   = $('#tabRow');
            this.dataGrid = new xhDataGrid($('#list'), 'lm/housing/getHouse');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_del']);
            this.funObj().bindDataGridEvent();
            this.listener();
        },
        listener:function () {
            var $this = this;

            //新增事件按钮
            $('#bt_list_add').on('click',function () {
                $this.funObj().openAddForm();
            });

            $('#bt_list_del').on('click',function () {
                $this.funObj().askDelQuestion();
            });

            $('#tabReturn').on('click',function () {
                $this.funObj().goToList();
            });
            $this.funObj().bindArea();//省县市三级联动

            $('#bt_list_search').click(function () {
                $this.funObj().search();
            });
        },
        funObj:function () {
            var $this = this;
            return {
                bindDataGridEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                        $('#title').contents()[0]['data'] = rowData['sAddress'];
                        window.frames['tabFrame'].location.href = baseUrl+'lm/housing/houseTab/'+rowData['iRHId'];
                        $this.titleRow.show();
                        $this.tabRow.show();
                        xhExtra.forwardToForm(true);
                    }
                },
                openAddForm:function () {
                    window.top.xhTop.showModal({
                        title: '新增住房信息',
                        content: '<iframe  src="'+baseUrl+'lm/housing/addForm"  style="width: 100%;;border-style: none;padding: 0px;height:360px"  scrolling="yes"   ></iframe>',
                        width:900,
                        button:'提交',
                        backClose:false
                    });
                },
                goToList:function () {
                    $this.titleRow.hide();
                    $this.tabRow.hide();
                    $this.dataGrid.reload();
                    xhExtra.backToList();
                },
                askDelQuestion:function () {
                    var id = $this.dataGrid.getSelectValues('iRHId');
                    if(id.length == 0){
                        window.top.xhTop.showError('请选中要删除的项');
                    }else{
                        xhExtra.askChangeStatus('lm/housing/delHouse',{id:id},null,null,function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.dataGrid.reload();
                        });
                    }
                },
                bindArea:function () {
                    // 城市选择
                    // $('#select_province').combobox({
                    //     editable:true,
                    //     filter: function(q, row){
                    //         var opts = $(this).combobox('options');
                    //         return row[opts.textField].indexOf(q) >= 0;
                    //     },
                    //     onSelect:function (record) {
                    //         if(record.value != '') {
                    //             $('#select_city').combobox({
                    //                 url: baseUrl + 'customer/customer/getAreaById/' + record.value,
                    //                 valueField: 'areaCode',
                    //                 textField: 'areaName',
                    //                 editable:true,
                    //                 filter: function(q, row){
                    //                     var opts = $(this).combobox('options');
                    //                     return row[opts.textField].indexOf(q) >= 0;
                    //                 },
                    //                 onSelect: function (record2) {
                    //                     if(record2.areaCode != ''){
                    //                         $('#select_county').combobox({
                    //                             url: baseUrl + 'customer/customer/getAreaById/' + record2.areaCode,
                    //                             valueField: 'areaCode',
                    //                             textField: 'areaName',
                    //                             editable:true,
                    //                             filter: function(q, row){
                    //                                 var opts = $(this).combobox('options');
                    //                                 return row[opts.textField].indexOf(q) >= 0;
                    //                             },
                    //                         })
                    //                     }else {
                    //                         $('#select_county').combobox('setValue','');
                    //                     }
                    //                 }
                    //             })
                    //         }else {
                    //             $('#select_city').combobox('setValue','');
                    //             $('#select_county').combobox('setValue','');
                    //         }
                    //     }
                    // });

                    //运营部门绑定
                    $('#select_secArea').combobox({
                        onSelect:function (record) {
                            if(record.value) {
                                $('#select_thirdArea').combobox({
                                    url: baseUrl + 'lm/housing/findArea/' + record.value,
                                    valueField: 'id',
                                    textField: 'text'
                                })
                            } else {
                                $('#select_thirdArea').combobox('setValue', '--运营县--').combobox('loadData', {"total":0,rows:[] });
                            }
                        }
                    });

                    $('#select_province').combobox({
                        onSelect: function(param){
                            $('#select_city').combobox({
                                url: baseUrl + 'customer/customer/getAreaById/' + param.value,
                                valueField: 'areaCode',
                                textField: 'areaName'
                            });
                            $('#select_county').combobox('setValue', '--选择区--').combobox('loadData', {"total":0,rows:[] });
                        }
                    });
                    $('#select_city').combobox({
                        onSelect: function(param){
                            $('#select_county').combobox({
                                url: baseUrl + 'customer/customer/getAreaById/' + param.areaCode,
                                valueField: 'areaCode',
                                textField: 'areaName'
                            });
                        }
                    });
                },
                search:function () {
                    var key = xhExtra.getValue($('#list_search'));
                    var province  = $('#select_province').combobox('getText');
                    var city      = $('#select_city').combobox('getText');
                    var county    = $('#select_county').combobox('getText');
                    var secArea   = xhExtra.getValue($('#select_secArea'));
                    var thirdArea = xhExtra.getValue($('#select_thirdArea'));
                    $this.dataGrid.reload({key:key , province:province , city:city , county:county , secArea : secArea , thirdArea : thirdArea });

                }
            }
        }
    }
};